import Vue from 'vue'
import MessageBox from './MessageBox.vue'
import LoadingBox from './LoadingBox.vue'
import ButtonBox from './ButtonBox.vue'
import UploadBox from './UploadBox.vue'
import DialogBox from './DialogBox.vue'
import PaginationBox from './PaginationBox.vue'
import TagsInput from './TagsInput.vue'

// 注册UI组件
Vue.component('ButtonBox', ButtonBox)
Vue.component('UploadBox', UploadBox)
Vue.component('DialogBox', DialogBox)
Vue.component('PaginationBox', PaginationBox)
Vue.component('TagsInput', TagsInput)

// 创建消息提示组件实例
const messageBoxInstance = new Vue({
  render: h => h(MessageBox)
}).$mount()

// 创建加载组件实例
const loadingBoxInstance = new Vue({
  render: h => h(LoadingBox)
}).$mount()

// 获取组件实例
const messageBoxComponent = messageBoxInstance.$children[0]
const loadingBoxComponent = loadingBoxInstance.$children[0]

// 创建消息提示服务
const messageBoxService = {
  success(message, options = {}) {
    return messageBoxComponent.success(message, options)
  },
  error(message, options = {}) {
    return messageBoxComponent.error(message, options)
  },
  warning(message, options = {}) {
    return messageBoxComponent.warning(message, options)
  },
  info(message, options = {}) {
    return messageBoxComponent.info(message, options)
  },
  confirm(message, title, options = {}) {
    return messageBoxComponent.confirm(message, title, options)
  },
  deleteConfirm(message, options = {}) {
    return messageBoxComponent.deleteConfirm(message, options)
  },
  prompt(message, title, options = {}) {
    return messageBoxComponent.prompt(message, title, options)
  },
  notify(message, title, options = {}) {
    return messageBoxComponent.notify(message, title, options)
  },
  notifySuccess(message, title, options = {}) {
    return messageBoxComponent.notifySuccess(message, title, options)
  },
  notifyError(message, title, options = {}) {
    return messageBoxComponent.notifyError(message, title, options)
  },
  notifyWarning(message, title, options = {}) {
    return messageBoxComponent.notifyWarning(message, title, options)
  },
  closeAll() {
    return messageBoxComponent.closeAll()
  }
}

// 创建加载服务
const loadingService = {
  show(text, options = {}) {
    return loadingBoxComponent.show(text, options)
  },
  showLocal(text, options = {}) {
    return loadingBoxComponent.showLocal(text, options)
  },
  hide() {
    return loadingBoxComponent.hide()
  },
  setText(text) {
    return loadingBoxComponent.setText(text)
  },
  async showWithPromise(promise, text, options = {}) {
    return loadingBoxComponent.showWithPromise(promise, text, options)
  }
}

// 注册全局方法
Vue.prototype.$messageBox = messageBoxService
Vue.prototype.$loadingBox = loadingService

// 安装函数
const install = function(Vue) {
  // 判断是否安装
  if (install.installed) return
}

// 判断是否是直接引入文件
if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}

export default {
  install,
  messageBoxService,
  loadingService
} 